//
// Created by hps on 24-4-6.
//
#include "MarkdownWindow.h"

namespace hps {
MarkdownWindow::MarkdownWindow()
    : EditorBase() {
    init();
    setUi();
}
MarkdownWindow::MarkdownWindow(std::string &path)
    : EditorBase(path) {
    m_previewEditor = new QTextEdit();
    m_highlight = new hps::MarkdownHighlight(m_textEdit->document());
    init();
    setUi();
    QObject::connect(m_textEdit, &TextEdit::textChanged, this, &MarkdownWindow::previewChange);
}
void MarkdownWindow::init() {
    m_previewEditor->setReadOnly(true);
}
void MarkdownWindow::setUi() {
    m_editorLayout->addWidget(m_previewEditor);
}
void MarkdownWindow::showPreview() {
    if (m_previewEditor->isHidden())
        m_previewEditor->show();
}
void MarkdownWindow::hidePreview() {
    if (! m_previewEditor->isHidden())
        m_previewEditor->hide();
}
void MarkdownWindow::previewChange() {
    if (m_textEdit->toPlainText().isEmpty())
        return;
    MarkDownParser *transform = new hps::MarkDownParser();
    transform->transform(m_textEdit->toPlainText().toStdString());
    m_previewEditor->setHtml(transform->getHtml().c_str());
    m_previewEditor->update();
    delete transform;
}
void MarkdownWindow::showTextEdit() {
    if (m_textEdit->isHidden())
        m_textEdit->show();
}
void MarkdownWindow::hideTextEdit() {
    if (! m_textEdit->isHidden())
        m_textEdit->hide();
}
void MarkdownWindow::showPreviewAndTextEdit() {
    hideTextEdit();
    hidePreview();
    showPreview();
    showTextEdit();
}
} // hps